#include "tore.h"

Tore::Tore() : SolidObject(), Lrayon(0.0), Srayon(0.0)
{

}

Tore::~Tore()
{

}

Tore::Tore(const Tore &tore) : SolidObject(tore), Lrayon(tore.Lrayon), Srayon(tore.Srayon)
{

}

Tore& Tore::operator=(const Tore &tore)
{
    ((SolidObject*)this)->operator=(tore);
    Lrayon = tore.Lrayon;
    Srayon = tore.Srayon;

    return (*this);

}
Tore::Tore(const Point3D &position, const double &_Lrayon, const double &_Srayon, const Color &_material) :
    SolidObject(position,_material), Lrayon(_Lrayon), Srayon(_Srayon)
{
}

inline void Tore::texturing(const Intersection &inter)
{
    Vector3D planToPoint(inter - (*this));
    double X(planToPoint*XVec), Y(planToPoint*YVec);
    texture->getColorAt(X,Y,material);
}
inline void Tore::intersection(const Ray &ray,Intersection &inter) const
{
    /*double rr(Srayon*Srayon - Lrayon*Lrayon);
    double sr2(Srayon*Srayon);
    double p2( ray.getOrigin().getX()*ray.getOrigin().getX()
             + ray.getOrigin().getY()*ray.getOrigin().getY()
             + ray.getOrigin().getZ()*ray.getOrigin().getZ());
    double p2rr = p2 + rr;
    double a = ray.getDirection().norm2();
    double b = ray.getDirection().getX()*ray.getOrigin().getX()
             + ray.getDirection().getY()*ray.getOrigin().getY()
             + ray.getDirection().getZ()*ray.getOrigin().getZ();

    double c = 2*a*p2rr  + b*b - 4*sr2*(b-ray.getDirection().getY()*ray.getOrigin().getY());
    double d = 4*b*p2rr  - 8*sr2*(a-ray.getDirection().getY()*ray.getOrigin().getY());
    double e = p2rr*p2rr - 4*sr2*(p2-ray.getOrigin().getY()*ray.getOrigin().getY());

    b = 4*a*b;
    a = a*a;*/



}

inline void Tore::getNormalVectorAt(const Point3D &inter, Vector3D &normal) const
{
    double  x(inter.getX()-getX()),
            y(inter.getY()-getY()),
            z(inter.getZ()-getZ()),
            p1(4*(x*x + y*y + z*z + Srayon*Srayon - Lrayon*Lrayon)),
            p2(8*Srayon*Srayon);
    normal.setX(p1*x - p2*x);
    normal.setY(p1*y);
    normal.setZ(p1*z - p2*z);
    normal.normalize();
}
